@echo off & setlocal

GOTO :init

:header
    echo %DOCENTRICAX_NAME% v%DOCENTRICAX_VERSION%
    echo Docentric AX AOS build tool automates model build and database synchronize procedure.
	echo.
	echo ** Detected D365FO Paths and tools	
	echo   D365FO AOS Path: %DOCENTRICAX_AOSPath%
	echo   D365FO AOS XPPBP Tool: %DOCENTRICAX_AOSTOOL_XPPBP%
	echo   D365FO AOS XPPC Tool: %DOCENTRICAX_AOSTOOL_XPPC%
	echo   D365FO AOS LABELC Tool: %DOCENTRICAX_AOSTOOL_LABELC%
	echo   D365FO AOS ReportsC Tool: %DOCENTRICAX_AOSTOOL_ReportsC%
	echo   D365FO AOS Database Sync Engine Tool: %DOCENTRICAX_AOSTOOL_DBSyncEngine%
	echo   D365FO Deploy reports tool: %DOCENTRICAX_AOSTOOL_DEPLOYALLREPORTSTOSSRS%
	echo.
	echo ** Detected models
	echo   %DOCENTRICAX_MODEL_MAIN_MODELNAME%=%DOCENTRICAX_MODEL_MAIN_PATH_EXISTS% (Version:%DOCENTRICAX_MODEL_MAIN_VERSION%; Path=%DOCENTRICAX_MODEL_MAIN_PATH%)
	echo   %DOCENTRICAX_MODEL_EXTENSION_MODELNAME%=%DOCENTRICAX_MODEL_EXTENSION_PATH_EXISTS% (Version:%DOCENTRICAX_MODEL_EXTENSION_VERSION%; Path=%DOCENTRICAX_MODEL_EXTENSION_PATH%)
	echo   %DOCENTRICAX_MODEL_EMAILS_MODELNAME%=%DOCENTRICAX_MODEL_EMAILS_PATH_EXISTS% (Version:%DOCENTRICAX_MODEL_EMAILS_VERSION%; Path=%DOCENTRICAX_MODEL_EMAILS_PATH%)
	echo   %DOCENTRICAX_MODEL_SSRSREPLICAS_MODELNAME%=%DOCENTRICAX_MODEL_SSRSREPLICAS_PATH_EXISTS% (Version:%DOCENTRICAX_MODEL_SSRSREPLICAS_VERSION%; Path=%DOCENTRICAX_MODEL_SSRSREPLICAS_PATH%)
	echo   %DOCENTRICAX_MODEL_CBDREPLICAS_MODELNAME%=%DOCENTRICAX_MODEL_CBDREPLICAS_PATH_EXISTS% (Version:%DOCENTRICAX_MODEL_CBDREPLICAS_VERSION%; Path=%DOCENTRICAX_MODEL_CBDREPLICAS_PATH%)
	echo   %DOCENTRICAX_MODEL_WAREHOUSELABELS_MODELNAME%=%DOCENTRICAX_MODEL_WAREHOUSELABELS_PATH_EXISTS% (Version:%DOCENTRICAX_MODEL_WAREHOUSELABELS_VERSION%; Path=%DOCENTRICAX_MODEL_WAREHOUSELABELS_PATH%)
	echo.
    goto :eof

:usage
    echo USAGE:
    echo   %DOCENTRICAXNET__BAT_NAME% [flags]
    echo.
	echo.  ** NOTE: Please run the tool as administrator
	echo.
	echo.  --^> Available flags
	echo.  /?, --help                   shows this help
    echo.  /v, --version                shows the version
	echo.
    goto :eof

:version
    if "%~1"=="full" call :header & goto :eof
    echo %DOCENTRICAX_VERSION%
    goto :eof
	
:missing_aos
    call :header
    call :usage
    echo.
    echo *******************************************
    echo ****    MISSING "D365FO AOS FOLDER"    ****
    echo *******************************************
    echo.
    goto :eof
:missing_tools
    call :header
    call :usage
    echo.
    echo *******************************************
    echo **** MISSING "REQUIRED D365FO TOOLS"   ****
    echo *******************************************
    echo.
    goto :eof	

:missing_model
    call :header
    call :usage
    echo.
    echo *********************************************************
    echo ****      DOCENTRIC AX CORE MODELS ARE MISSING       ****
	echo ****  PLEASE INSTALL THE CORE MODELS AND RUN AGAIN.  ****
    echo *********************************************************
    echo.
	echo. %DOCENTRICAX_MODEL_MAIN_MODELNAME% present %DOCENTRICAX_MODEL_MAIN_PATH_EXISTS% on requested path %DOCENTRICAX_MODEL_MAIN_PATH%.
	echo. %DOCENTRICAX_MODEL_EXTENSION_MODELNAME% present %DOCENTRICAX_MODEL_EXTENSION_PATH_EXISTS% on requested path %DOCENTRICAX_MODEL_EXTENSION_PATH%.
	REM OPTIONAL echo. %DOCENTRICAX_MODEL_EMAILS_MODELNAME% present %DOCENTRICAX_MODEL_EMAILS_PATH_EXISTS% on requested path %DOCENTRICAX_MODEL_EMAILS_PATH%.
    goto :eof

:elevated_rights
	call :header
    echo.
    echo *********************************************************
    echo ****            ELEVATED RIGHTS REQUIRED             ****
	echo ****      PLEASE RUN THE TOOL AS ADMINISTRATOR.      ****
    echo *********************************************************
    echo.
	goto :eof

:init
	set "DOCENTRICAX_NAME=%~n0"
    set "DOCENTRICAX_VERSION=1.0"
    set "DOCENTRICAX_YEAR=2021"

    set "DOCENTRICAX__BAT_FILE=%~0"
    set "DOCENTRICAX__BAT_PATH=%~dp0"
    set "DOCENTRICAX__BAT_NAME=%~nx0"

    set "DOCENTRICAX_OptHelp="
    set "DOCENTRICAX_OptVersion="
    set "DOCENTRICAX_OptVerbose=no"
	set "DOCENTRICAX_OptQuiet=no"

	REM Find AOS Service path
	set "DOCENTRICAX_AOSPath=[path not found]"
	set "DOCENTRICAX_AOSWebRootPath=[path not found]"	
	
	if exist "C:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=C:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=C:\AosService\WebRoot"
	if exist "D:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=D:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=D:\AosService\WebRoot"
	if exist "E:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=E:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=E:\AosService\WebRoot"
	if exist "F:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=F:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=F:\AosService\WebRoot"
	if exist "G:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=G:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=G:\AosService\WebRoot"
	if exist "H:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=H:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=H:\AosService\WebRoot"
	if exist "I:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=I:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=I:\AosService\WebRoot"
	if exist "J:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=J:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=J:\AosService\WebRoot"
	if exist "K:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=K:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=K:\AosService\WebRoot"
	if exist "M:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=M:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=L:\AosService\WebRoot"
	if exist "N:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=N:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=M:\AosService\WebRoot"
	if exist "O:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=O:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=N:\AosService\WebRoot"
	if exist "P:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=P:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=O:\AosService\WebRoot"
	if exist "Q:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=Q:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=P:\AosService\WebRoot"
	if exist "R:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=R:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=R:\AosService\WebRoot"
	if exist "S:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=S:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=S:\AosService\WebRoot"
	if exist "T:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=T:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=T:\AosService\WebRoot"
	if exist "U:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=U:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=U:\AosService\WebRoot"
	if exist "V:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=V:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=V:\AosService\WebRoot"
	if exist "W:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=W:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=W:\AosService\WebRoot"
	if exist "X:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=X:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=X:\AosService\WebRoot"
	if exist "Y:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=Y:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=Y:\AosService\WebRoot"
	if exist "Z:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=Z:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=Z:\AosService\WebRoot"
	
	REM Init AOS Tools
	set "DOCENTRICAX_AOSTOOL_XPPBP=[tool is missing]"
	set "DOCENTRICAX_AOSTOOL_XPPC=[tool is missing]"
	set "DOCENTRICAX_AOSTOOL_LABELC=[tool is missing]"
	set "DOCENTRICAX_AOSTOOL_ReportsC=[tool is missing]"
	set "DOCENTRICAX_AOSTOOL_DBSyncEngine=[tool is missing]"
	set "DOCENTRICAX_AOSTOOL_DEPLOYALLREPORTSTOSSRS=[tool is missing]"

	REM Check if AOS path was found
    if not defined DOCENTRICAX_AOSPath call :missing_aos & goto :end
	if not exist "%DOCENTRICAX_AOSPath%" call :missing_aos & goto :end

	set "DOCENTRICAX_AOSPath_BINFOLDER=%DOCENTRICAX_AOSPath%\Bin"

	REM Set all tools
	set "DOCENTRICAX_AOSTOOL_XPPBP=%DOCENTRICAX_AOSPath%\Bin\xppbp.exe"
	set "DOCENTRICAX_AOSTOOL_XPPC=%DOCENTRICAX_AOSPath%\Bin\Xppc.exe"
	set "DOCENTRICAX_AOSTOOL_LABELC=%DOCENTRICAX_AOSPath%\Bin\LabelC.exe"
	set "DOCENTRICAX_AOSTOOL_ReportsC=%DOCENTRICAX_AOSPath%\Bin\ReportsC.exe"
	set "DOCENTRICAX_AOSTOOL_DBSyncEngine=%DOCENTRICAX_AOSPath%\Bin\SyncEngine.exe"
	set "DOCENTRICAX_AOSTOOL_DEPLOYALLREPORTSTOSSRS=%DOCENTRICAX_AOSPath%\Plugins\AxReportVmRoleStartupTask\DeployAllReportsToSsrs.ps1"
	
	REM Check if all tools exist
	if not defined DOCENTRICAX_AOSTOOL_XPPBP call :missing_tools & goto :end
	if not exist "%DOCENTRICAX_AOSTOOL_XPPBP%" call :missing_tools & goto :end
	if not defined DOCENTRICAX_AOSTOOL_XPPC call :missing_tools & goto :end
	if not exist "%DOCENTRICAX_AOSTOOL_XPPC%" call :missing_tools & goto :end
	if not defined DOCENTRICAX_AOSTOOL_LABELC call :missing_tools & goto :end
	if not exist "%DOCENTRICAX_AOSTOOL_LABELC%" call :missing_tools & goto :end
	if not defined DOCENTRICAX_AOSTOOL_ReportsC call :missing_tools & goto :end
	if not exist "%DOCENTRICAX_AOSTOOL_ReportsC%" call :missing_tools & goto :end
	if not defined DOCENTRICAX_AOSTOOL_DBSyncEngine call :missing_tools & goto :end
	if not exist "%DOCENTRICAX_AOSTOOL_DBSyncEngine%" call :missing_tools & goto :end
	if not defined DOCENTRICAX_AOSTOOL_DEPLOYALLREPORTSTOSSRS call :missing_tools & goto :end
	if not exist "%DOCENTRICAX_AOSTOOL_DEPLOYALLREPORTSTOSSRS%" call :missing_tools & goto :end

	REM Set Docentric modules
	SET "DOCENTRICAX_MODEL_MAIN_MODULENAME=DocentricAX"
	SET "DOCENTRICAX_MODEL_MAIN_MODELNAME=Docentric AX"
	SET "DOCENTRICAX_MODEL_MAIN_VERSION=Unknown"
	SET "DOCENTRICAX_MODEL_MAIN_PATH=%DOCENTRICAX_AOSPath%\%DOCENTRICAX_MODEL_MAIN_MODULENAME%"
	SET "DOCENTRICAX_MODEL_MAIN_LOG_PATH=%DOCENTRICAX_MODEL_MAIN_PATH%"
	SET "DOCENTRICAX_MODEL_MAIN_LOG_LABELC_OUTPUT_FILE=%DOCENTRICAX_MODEL_MAIN_LOG_PATH%\%DOCENTRICAX_MODEL_MAIN_MODELNAME%.CompileLabels.log"
	SET "DOCENTRICAX_MODEL_MAIN_LOG_LABELC_ERROUTPUT_FILE=%DOCENTRICAX_MODEL_MAIN_LOG_PATH%\%DOCENTRICAX_MODEL_MAIN_MODELNAME%.CompileLabels.err"
	SET "DOCENTRICAX_MODEL_MAIN_LOG_XPPC_OUTPUT_FILE=%DOCENTRICAX_MODEL_MAIN_LOG_PATH%\%DOCENTRICAX_MODEL_MAIN_MODELNAME%.BuildModelResult.log"
	SET "DOCENTRICAX_MODEL_MAIN_LOG_XPPC_XMLOUTPUT_FILE=%DOCENTRICAX_MODEL_MAIN_LOG_PATH%\%DOCENTRICAX_MODEL_MAIN_MODELNAME%.BuildModelResult.xml"
	SET "DOCENTRICAX_MODEL_MAIN_LOG_XPPBP_OUTPUT_FILE=%DOCENTRICAX_MODEL_MAIN_LOG_PATH%\%DOCENTRICAX_MODEL_MAIN_MODELNAME%.BestPracticeModelResult.log"
	SET "DOCENTRICAX_MODEL_MAIN_LOG_XPPBP_XMLOUTPUT_FILE=%DOCENTRICAX_MODEL_MAIN_LOG_PATH%\%DOCENTRICAX_MODEL_MAIN_MODELNAME%.BestPracticeModelResult.xml"
	SET "DOCENTRICAX_MODEL_MAIN_LOG_REPORTC_OUTPUT_FILE=%DOCENTRICAX_MODEL_MAIN_LOG_PATH%\%DOCENTRICAX_MODEL_MAIN_MODELNAME%.BuildSSRSReportResult.log"
	SET "DOCENTRICAX_MODEL_MAIN_LOG_REPORTC_XMLOUTPUT_FILE=%DOCENTRICAX_MODEL_MAIN_LOG_PATH%\%DOCENTRICAX_MODEL_MAIN_MODELNAME%.BuildSSRSReportResult.xml"
	SET "DOCENTRICAX_MODEL_MAIN_VERSION_DESCRIPTOR_FILE=%DOCENTRICAX_MODEL_MAIN_PATH%\Descriptor\%DOCENTRICAX_MODEL_MAIN_MODELNAME%.xml"
	SET "DOCENTRICAX_MODEL_MAIN_PATH_EXISTS=Yes"
	if not exist "%DOCENTRICAX_MODEL_MAIN_VERSION_DESCRIPTOR_FILE%" set "DOCENTRICAX_MODEL_MAIN_PATH_EXISTS=No" & goto skip_read_version_model_main
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionMajor>" ^< "%DOCENTRICAX_MODEL_MAIN_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionMajor=%%~a
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionMinor>" ^< "%DOCENTRICAX_MODEL_MAIN_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionMinor=%%~a
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionBuild>" ^< "%DOCENTRICAX_MODEL_MAIN_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionBuild=%%~a
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionRevision>" ^< "%DOCENTRICAX_MODEL_MAIN_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionRevision=%%~a
	set "DOCENTRICAX_MODEL_MAIN_VERSION=%DocentricAX_model_versionMajor%.%DocentricAX_model_versionMinor%.%DocentricAX_model_versionBuild%.%DocentricAX_model_versionRevision%"
	:skip_read_version_model_main
	
	SET "DOCENTRICAX_MODEL_EXTENSION_MODULENAME=DocentricAXExtension"
	SET "DOCENTRICAX_MODEL_EXTENSION_MODELNAME=Docentric AX Extension"
	SET "DOCENTRICAX_MODEL_EXTENSION_VERSION=Unknown"
	SET "DOCENTRICAX_MODEL_EXTENSION_PATH=%DOCENTRICAX_AOSPath%\%DOCENTRICAX_MODEL_EXTENSION_MODULENAME%"
	SET "DOCENTRICAX_MODEL_EXTENSION_LOG_PATH=%DOCENTRICAX_MODEL_EXTENSION_PATH%"
	SET "DOCENTRICAX_MODEL_EXTENSION_LOG_LABELC_OUTPUT_FILE=%DOCENTRICAX_MODEL_EXTENSION_LOG_PATH%\%DOCENTRICAX_MODEL_EXTENSION_MODELNAME%.CompileLabels.log"
	SET "DOCENTRICAX_MODEL_EXTENSION_LOG_LABELC_ERROUTPUT_FILE=%DOCENTRICAX_MODEL_EXTENSION_LOG_PATH%\%DOCENTRICAX_MODEL_EXTENSION_MODELNAME%.CompileLabels.err"
	SET "DOCENTRICAX_MODEL_EXTENSION_LOG_XPPC_OUTPUT_FILE=%DOCENTRICAX_MODEL_EXTENSION_LOG_PATH%\%DOCENTRICAX_MODEL_EXTENSION_MODELNAME%.BuildModelResult.log"
	SET "DOCENTRICAX_MODEL_EXTENSION_LOG_XPPC_XMLOUTPUT_FILE=%DOCENTRICAX_MODEL_EXTENSION_LOG_PATH%\%DOCENTRICAX_MODEL_EXTENSION_MODELNAME%.BuildModelResult.xml"
	SET "DOCENTRICAX_MODEL_EXTENSION_LOG_XPPBP_OUTPUT_FILE=%DOCENTRICAX_MODEL_EXTENSION_LOG_PATH%\%DOCENTRICAX_MODEL_EXTENSION_MODELNAME%.BestPracticeModelResult.log"
	SET "DOCENTRICAX_MODEL_EXTENSION_LOG_XPPBP_XMLOUTPUT_FILE=%DOCENTRICAX_MODEL_EXTENSION_LOG_PATH%\%DOCENTRICAX_MODEL_EXTENSION_MODELNAME%.BestPracticeModelResult.xml"
	SET "DOCENTRICAX_MODEL_EXTENSION_LOG_REPORTC_OUTPUT_FILE=%DOCENTRICAX_MODEL_EXTENSION_LOG_PATH%\%DOCENTRICAX_MODEL_EXTENSION_MODELNAME%.BuildSSRSReportResult.log"
	SET "DOCENTRICAX_MODEL_EXTENSION_LOG_REPORTC_XMLOUTPUT_FILE=%DOCENTRICAX_MODEL_EXTENSION_LOG_PATH%\%DOCENTRICAX_MODEL_EXTENSION_MODELNAME%.BuildSSRSReportResult.xml"	
	SET "DOCENTRICAX_MODEL_EXTENSION_VERSION_DESCRIPTOR_FILE=%DOCENTRICAX_MODEL_EXTENSION_PATH%\Descriptor\%DOCENTRICAX_MODEL_EXTENSION_MODELNAME%.xml"
	SET "DOCENTRICAX_MODEL_EXTENSION_PATH_EXISTS=Yes"
	if not exist "%DOCENTRICAX_MODEL_EXTENSION_VERSION_DESCRIPTOR_FILE%" set "DOCENTRICAX_MODEL_EXTENSION_PATH_EXISTS=No" & goto skip_read_version_model_extension
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionMajor>" ^< "%DOCENTRICAX_MODEL_EXTENSION_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionMajor=%%~a
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionMinor>" ^< "%DOCENTRICAX_MODEL_EXTENSION_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionMinor=%%~a
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionBuild>" ^< "%DOCENTRICAX_MODEL_EXTENSION_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionBuild=%%~a
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionRevision>" ^< "%DOCENTRICAX_MODEL_EXTENSION_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionRevision=%%~a
	set "DOCENTRICAX_MODEL_EXTENSION_VERSION=%DocentricAX_model_versionMajor%.%DocentricAX_model_versionMinor%.%DocentricAX_model_versionBuild%.%DocentricAX_model_versionRevision%"
	:skip_read_version_model_extension

	SET "DOCENTRICAX_MODEL_EMAILS_MODULENAME=DocentricAXEmails"
	SET "DOCENTRICAX_MODEL_EMAILS_MODELNAME=Docentric AX Emails"
	SET "DOCENTRICAX_MODEL_EMAILS_VERSION=Unknown"
	SET "DOCENTRICAX_MODEL_EMAILS_PATH=%DOCENTRICAX_AOSPath%\%DOCENTRICAX_MODEL_EMAILS_MODULENAME%"
	SET "DOCENTRICAX_MODEL_EMAILS_LOG_PATH=%DOCENTRICAX_MODEL_EMAILS_PATH%"
	SET "DOCENTRICAX_MODEL_EMAILS_LOG_LABELC_OUTPUT_FILE=%DOCENTRICAX_MODEL_EMAILS_LOG_PATH%\%DOCENTRICAX_MODEL_EMAILS_MODELNAME%.CompileLabels.log"
	SET "DOCENTRICAX_MODEL_EMAILS_LOG_LABELC_ERROUTPUT_FILE=%DOCENTRICAX_MODEL_EMAILS_LOG_PATH%\%DOCENTRICAX_MODEL_EMAILS_MODELNAME%.CompileLabels.err"
	SET "DOCENTRICAX_MODEL_EMAILS_LOG_XPPC_OUTPUT_FILE=%DOCENTRICAX_MODEL_EMAILS_LOG_PATH%\%DOCENTRICAX_MODEL_EMAILS_MODELNAME%.BuildModelResult.log"
	SET "DOCENTRICAX_MODEL_EMAILS_LOG_XPPC_XMLOUTPUT_FILE=%DOCENTRICAX_MODEL_EMAILS_LOG_PATH%\%DOCENTRICAX_MODEL_EMAILS_MODELNAME%.BuildModelResult.xml"
	SET "DOCENTRICAX_MODEL_EMAILS_LOG_XPPBP_OUTPUT_FILE=%DOCENTRICAX_MODEL_EMAILS_LOG_PATH%\%DOCENTRICAX_MODEL_EMAILS_MODELNAME%.BestPracticeModelResult.log"
	SET "DOCENTRICAX_MODEL_EMAILS_LOG_XPPBP_XMLOUTPUT_FILE=%DOCENTRICAX_MODEL_EMAILS_LOG_PATH%\%DOCENTRICAX_MODEL_EMAILS_MODELNAME%.BestPracticeModelResult.xml"
	SET "DOCENTRICAX_MODEL_EMAILS_LOG_REPORTC_OUTPUT_FILE=%DOCENTRICAX_MODEL_EMAILS_LOG_PATH%\%DOCENTRICAX_MODEL_EMAILS_MODELNAME%.BuildSSRSReportResult.log"
	SET "DOCENTRICAX_MODEL_EMAILS_LOG_REPORTC_XMLOUTPUT_FILE=%DOCENTRICAX_MODEL_EMAILS_LOG_PATH%\%DOCENTRICAX_MODEL_EMAILS_MODELNAME%.BuildSSRSReportResult.xml"
	SET "DOCENTRICAX_MODEL_EMAILS_VERSION_DESCRIPTOR_FILE=%DOCENTRICAX_MODEL_EMAILS_PATH%\Descriptor\%DOCENTRICAX_MODEL_EMAILS_MODELNAME%.xml"
	SET "DOCENTRICAX_MODEL_EMAILS_PATH_EXISTS=Yes"
	if not exist "%DOCENTRICAX_MODEL_EMAILS_VERSION_DESCRIPTOR_FILE%" SET "DOCENTRICAX_MODEL_EMAILS_PATH_EXISTS=No" & goto skip_read_version_model_emails
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionMajor>" ^< "%DOCENTRICAX_MODEL_EMAILS_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionMajor=%%~a
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionMinor>" ^< "%DOCENTRICAX_MODEL_EMAILS_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionMinor=%%~a
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionBuild>" ^< "%DOCENTRICAX_MODEL_EMAILS_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionBuild=%%~a
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionRevision>" ^< "%DOCENTRICAX_MODEL_EMAILS_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionRevision=%%~a
	set "DOCENTRICAX_MODEL_EMAILS_VERSION=%DocentricAX_model_versionMajor%.%DocentricAX_model_versionMinor%.%DocentricAX_model_versionBuild%.%DocentricAX_model_versionRevision%"
	:skip_read_version_model_emails
	
	SET "DOCENTRICAX_MODEL_SSRSREPLICAS_MODULENAME=DocentricAXSSRSReplicas"
	SET "DOCENTRICAX_MODEL_SSRSREPLICAS_MODELNAME=Docentric AX SSRS Replicas"
	SET "DOCENTRICAX_MODEL_SSRSREPLICAS_VERSION=Unknown"
	SET "DOCENTRICAX_MODEL_SSRSREPLICAS_PATH=%DOCENTRICAX_AOSPath%\%DOCENTRICAX_MODEL_SSRSREPLICAS_MODULENAME%"
	SET "DOCENTRICAX_MODEL_SSRSREPLICAS_LOG_PATH=%DOCENTRICAX_MODEL_SSRSREPLICAS_PATH%"
	SET "DOCENTRICAX_MODEL_SSRSREPLICAS_LOG_LABELC_OUTPUT_FILE=%DOCENTRICAX_MODEL_SSRSREPLICAS_LOG_PATH%\%DOCENTRICAX_MODEL_SSRSREPLICAS_MODELNAME%.CompileLabels.log"
	SET "DOCENTRICAX_MODEL_SSRSREPLICAS_LOG_LABELC_ERROUTPUT_FILE=%DOCENTRICAX_MODEL_SSRSREPLICAS_LOG_PATH%\%DOCENTRICAX_MODEL_SSRSREPLICAS_MODELNAME%.CompileLabels.err"
	SET "DOCENTRICAX_MODEL_SSRSREPLICAS_LOG_XPPC_OUTPUT_FILE=%DOCENTRICAX_MODEL_SSRSREPLICAS_LOG_PATH%\%DOCENTRICAX_MODEL_SSRSREPLICAS_MODELNAME%.BuildModelResult.log"
	SET "DOCENTRICAX_MODEL_SSRSREPLICAS_LOG_XPPC_XMLOUTPUT_FILE=%DOCENTRICAX_MODEL_SSRSREPLICAS_LOG_PATH%\%DOCENTRICAX_MODEL_SSRSREPLICAS_MODELNAME%.BuildModelResult.xml"
	SET "DOCENTRICAX_MODEL_SSRSREPLICAS_LOG_XPPBP_OUTPUT_FILE=%DOCENTRICAX_MODEL_SSRSREPLICAS_LOG_PATH%\%DOCENTRICAX_MODEL_SSRSREPLICAS_MODELNAME%.BestPracticeModelResult.log"
	SET "DOCENTRICAX_MODEL_SSRSREPLICAS_LOG_XPPBP_XMLOUTPUT_FILE=%DOCENTRICAX_MODEL_SSRSREPLICAS_LOG_PATH%\%DOCENTRICAX_MODEL_SSRSREPLICAS_MODELNAME%.BestPracticeModelResult.xml"
	SET "DOCENTRICAX_MODEL_SSRSREPLICAS_LOG_REPORTC_OUTPUT_FILE=%DOCENTRICAX_MODEL_SSRSREPLICAS_LOG_PATH%\%DOCENTRICAX_MODEL_SSRSREPLICAS_MODELNAME%.BuildSSRSReportResult.log"
	SET "DOCENTRICAX_MODEL_SSRSREPLICAS_LOG_REPORTC_XMLOUTPUT_FILE=%DOCENTRICAX_MODEL_SSRSREPLICAS_LOG_PATH%\%DOCENTRICAX_MODEL_SSRSREPLICAS_MODELNAME%.BuildSSRSReportResult.xml"	
	SET "DOCENTRICAX_MODEL_SSRSREPLICAS_VERSION_DESCRIPTOR_FILE=%DOCENTRICAX_MODEL_SSRSREPLICAS_PATH%\Descriptor\%DOCENTRICAX_MODEL_SSRSREPLICAS_MODELNAME%.xml"
	SET "DOCENTRICAX_MODEL_SSRSREPLICAS_PATH_EXISTS=Yes"
	if not exist "%DOCENTRICAX_MODEL_SSRSREPLICAS_VERSION_DESCRIPTOR_FILE%" SET "DOCENTRICAX_MODEL_SSRSREPLICAS_PATH_EXISTS=No" & goto skip_read_version_model_ssrsreplicas
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionMajor>" ^< "%DOCENTRICAX_MODEL_SSRSREPLICAS_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionMajor=%%~a
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionMinor>" ^< "%DOCENTRICAX_MODEL_SSRSREPLICAS_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionMinor=%%~a
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionBuild>" ^< "%DOCENTRICAX_MODEL_SSRSREPLICAS_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionBuild=%%~a
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionRevision>" ^< "%DOCENTRICAX_MODEL_SSRSREPLICAS_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionRevision=%%~a
	set "DOCENTRICAX_MODEL_SSRSREPLICAS_VERSION=%DocentricAX_model_versionMajor%.%DocentricAX_model_versionMinor%.%DocentricAX_model_versionBuild%.%DocentricAX_model_versionRevision%"
	:skip_read_version_model_ssrsreplicas
	
	SET "DOCENTRICAX_MODEL_CBDREPLICAS_MODULENAME=DocentricAXCBDReplicas"
	SET "DOCENTRICAX_MODEL_CBDREPLICAS_MODELNAME=Docentric AX CBD Replicas"
	SET "DOCENTRICAX_MODEL_CBDREPLICAS_VERSION=Unknown"
	SET "DOCENTRICAX_MODEL_CBDREPLICAS_PATH=%DOCENTRICAX_AOSPath%\%DOCENTRICAX_MODEL_CBDREPLICAS_MODULENAME%"
	SET "DOCENTRICAX_MODEL_CBDREPLICAS_LOG_PATH=%DOCENTRICAX_MODEL_CBDREPLICAS_PATH%"
	SET "DOCENTRICAX_MODEL_CBDREPLICAS_LOG_LABELC_OUTPUT_FILE=%DOCENTRICAX_MODEL_CBDREPLICAS_LOG_PATH%\%DOCENTRICAX_MODEL_CBDREPLICAS_MODELNAME%.CompileLabels.log"
	SET "DOCENTRICAX_MODEL_CBDREPLICAS_LOG_LABELC_ERROUTPUT_FILE=%DOCENTRICAX_MODEL_CBDREPLICAS_LOG_PATH%\%DOCENTRICAX_MODEL_CBDREPLICAS_MODELNAME%.CompileLabels.err"
	SET "DOCENTRICAX_MODEL_CBDREPLICAS_LOG_XPPC_OUTPUT_FILE=%DOCENTRICAX_MODEL_CBDREPLICAS_LOG_PATH%\%DOCENTRICAX_MODEL_CBDREPLICAS_MODELNAME%.BuildModelResult.log"
	SET "DOCENTRICAX_MODEL_CBDREPLICAS_LOG_XPPC_XMLOUTPUT_FILE=%DOCENTRICAX_MODEL_CBDREPLICAS_LOG_PATH%\%DOCENTRICAX_MODEL_CBDREPLICAS_MODELNAME%.BuildModelResult.xml"
	SET "DOCENTRICAX_MODEL_CBDREPLICAS_LOG_XPPBP_OUTPUT_FILE=%DOCENTRICAX_MODEL_CBDREPLICAS_LOG_PATH%\%DOCENTRICAX_MODEL_CBDREPLICAS_MODELNAME%.BestPracticeModelResult.log"
	SET "DOCENTRICAX_MODEL_CBDREPLICAS_LOG_XPPBP_XMLOUTPUT_FILE=%DOCENTRICAX_MODEL_CBDREPLICAS_LOG_PATH%\%DOCENTRICAX_MODEL_CBDREPLICAS_MODELNAME%.BestPracticeModelResult.xml"
	SET "DOCENTRICAX_MODEL_CBDREPLICAS_LOG_REPORTC_OUTPUT_FILE=%DOCENTRICAX_MODEL_CBDREPLICAS_LOG_PATH%\%DOCENTRICAX_MODEL_CBDREPLICAS_MODELNAME%.BuildSSRSReportResult.log"
	SET "DOCENTRICAX_MODEL_CBDREPLICAS_LOG_REPORTC_XMLOUTPUT_FILE=%DOCENTRICAX_MODEL_CBDREPLICAS_LOG_PATH%\%DOCENTRICAX_MODEL_CBDREPLICAS_MODELNAME%.BuildSSRSReportResult.xml"
	SET "DOCENTRICAX_MODEL_CBDREPLICAS_VERSION_DESCRIPTOR_FILE=%DOCENTRICAX_MODEL_CBDREPLICAS_PATH%\Descriptor\%DOCENTRICAX_MODEL_CBDREPLICAS_MODELNAME%.xml"
	SET "DOCENTRICAX_MODEL_CBDREPLICAS_PATH_EXISTS=Yes"
	if not exist "%DOCENTRICAX_MODEL_CBDREPLICAS_VERSION_DESCRIPTOR_FILE%" SET "DOCENTRICAX_MODEL_CBDREPLICAS_PATH_EXISTS=No" & goto skip_read_version_model_cbdreplicas
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionMajor>" ^< "%DOCENTRICAX_MODEL_CBDREPLICAS_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionMajor=%%~a
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionMinor>" ^< "%DOCENTRICAX_MODEL_CBDREPLICAS_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionMinor=%%~a
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionBuild>" ^< "%DOCENTRICAX_MODEL_CBDREPLICAS_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionBuild=%%~a
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionRevision>" ^< "%DOCENTRICAX_MODEL_CBDREPLICAS_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionRevision=%%~a
	set "DOCENTRICAX_MODEL_CBDREPLICAS_VERSION=%DocentricAX_model_versionMajor%.%DocentricAX_model_versionMinor%.%DocentricAX_model_versionBuild%.%DocentricAX_model_versionRevision%"
	:skip_read_version_model_cbdreplicas

	SET "DOCENTRICAX_MODEL_WAREHOUSELABELS_MODULENAME=DocentricAXWarehouseLabels"
	SET "DOCENTRICAX_MODEL_WAREHOUSELABELS_MODELNAME=Docentric AX Warehouse Labels"
	SET "DOCENTRICAX_MODEL_WAREHOUSELABELS_VERSION=Unknown"
	SET "DOCENTRICAX_MODEL_WAREHOUSELABELS_PATH=%DOCENTRICAX_AOSPath%\%DOCENTRICAX_MODEL_WAREHOUSELABELS_MODULENAME%"
	SET "DOCENTRICAX_MODEL_WAREHOUSELABELS_LOG_PATH=%DOCENTRICAX_MODEL_WAREHOUSELABELS_PATH%"
	SET "DOCENTRICAX_MODEL_WAREHOUSELABELS_LOG_LABELC_OUTPUT_FILE=%DOCENTRICAX_MODEL_WAREHOUSELABELS_LOG_PATH%\%DOCENTRICAX_MODEL_WAREHOUSELABELS_MODELNAME%.CompileLabels.log"
	SET "DOCENTRICAX_MODEL_WAREHOUSELABELS_LOG_LABELC_ERROUTPUT_FILE=%DOCENTRICAX_MODEL_WAREHOUSELABELS_LOG_PATH%\%DOCENTRICAX_MODEL_WAREHOUSELABELS_MODELNAME%.CompileLabels.err"
	SET "DOCENTRICAX_MODEL_WAREHOUSELABELS_LOG_XPPC_OUTPUT_FILE=%DOCENTRICAX_MODEL_WAREHOUSELABELS_LOG_PATH%\%DOCENTRICAX_MODEL_WAREHOUSELABELS_MODELNAME%.BuildModelResult.log"
	SET "DOCENTRICAX_MODEL_WAREHOUSELABELS_LOG_XPPC_XMLOUTPUT_FILE=%DOCENTRICAX_MODEL_WAREHOUSELABELS_LOG_PATH%\%DOCENTRICAX_MODEL_WAREHOUSELABELS_MODELNAME%.BuildModelResult.xml"
	SET "DOCENTRICAX_MODEL_WAREHOUSELABELS_LOG_XPPBP_OUTPUT_FILE=%DOCENTRICAX_MODEL_WAREHOUSELABELS_LOG_PATH%\%DOCENTRICAX_MODEL_WAREHOUSELABELS_MODELNAME%.BestPracticeModelResult.log"
	SET "DOCENTRICAX_MODEL_WAREHOUSELABELS_LOG_XPPBP_XMLOUTPUT_FILE=%DOCENTRICAX_MODEL_WAREHOUSELABELS_LOG_PATH%\%DOCENTRICAX_MODEL_WAREHOUSELABELS_MODELNAME%.BestPracticeModelResult.xml"
	SET "DOCENTRICAX_MODEL_WAREHOUSELABELS_LOG_REPORTC_OUTPUT_FILE=%DOCENTRICAX_MODEL_WAREHOUSELABELS_LOG_PATH%\%DOCENTRICAX_MODEL_WAREHOUSELABELS_MODELNAME%.BuildSSRSReportResult.log"
	SET "DOCENTRICAX_MODEL_WAREHOUSELABELS_LOG_REPORTC_XMLOUTPUT_FILE=%DOCENTRICAX_MODEL_WAREHOUSELABELS_LOG_PATH%\%DOCENTRICAX_MODEL_WAREHOUSELABELS_MODELNAME%.BuildSSRSReportResult.xml"
	SET "DOCENTRICAX_MODEL_WAREHOUSELABELS_LOG_DEPLOYALLREPORTSTOSSRS_OUTPUT_FILE=%DOCENTRICAX_MODEL_WAREHOUSELABELS_LOG_PATH%\%DOCENTRICAX_MODEL_WAREHOUSELABELS_MODELNAME%.DeployReportsToSSRSResult.log"
	SET "DOCENTRICAX_MODEL_WAREHOUSELABELS_VERSION_DESCRIPTOR_FILE=%DOCENTRICAX_MODEL_WAREHOUSELABELS_PATH%\Descriptor\%DOCENTRICAX_MODEL_WAREHOUSELABELS_MODELNAME%.xml"
	SET "DOCENTRICAX_MODEL_WAREHOUSELABELS_PATH_EXISTS=Yes"
	if not exist "%DOCENTRICAX_MODEL_WAREHOUSELABELS_VERSION_DESCRIPTOR_FILE%" set "DOCENTRICAX_MODEL_WAREHOUSELABELS_PATH_EXISTS=No" & goto skip_read_version_model_warehouselabels
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionMajor>" ^< "%DOCENTRICAX_MODEL_WAREHOUSELABELS_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionMajor=%%~a
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionMinor>" ^< "%DOCENTRICAX_MODEL_WAREHOUSELABELS_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionMinor=%%~a
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionBuild>" ^< "%DOCENTRICAX_MODEL_WAREHOUSELABELS_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionBuild=%%~a
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionRevision>" ^< "%DOCENTRICAX_MODEL_WAREHOUSELABELS_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionRevision=%%~a
	set "DOCENTRICAX_MODEL_WAREHOUSELABELS_VERSION=%DocentricAX_model_versionMajor%.%DocentricAX_model_versionMinor%.%DocentricAX_model_versionBuild%.%DocentricAX_model_versionRevision%"
	:skip_read_version_model_warehouselabels
	
	REM Database hostname
	FOR /F "usebackq" %%i IN (`hostname`) DO SET "DOCENTRICAX_DB_HOSTNAME=%%i"
	set "DOCENTRIC_AX_DBSYNC_LOGFILE=%DOCENTRICAX_MODEL_MAIN_LOG_PATH%\DatabaseSync.log"
	
	REM Detect if AOS applications are running (IIS, IISExpress, Batch.exe)
	set "DOCENTRICAX_AOSWEB_RUNNING=No"
	set "DOCENTRICAX_AOSBATCH_RUNNING=No"
	tasklist /fi "IMAGENAME eq w3wp.exe" | find /i "w3wp.exe">NUL
	if %ERRORLEVEL% equ 0 set "DOCENTRICAX_AOSWEB_RUNNING=Yes"
	tasklist /fi "IMAGENAME eq iisexpress.exe" | find /i "iisexpress.exe">NUL
	if %ERRORLEVEL% equ 0 set "DOCENTRICAX_AOSWEB_RUNNING=Yes"
	REM if none of the exe files are running maybe IIS is waiting for the first command
	for /f "tokens=4" %%a in ('sc query W3SVC ^| find "STATE"') do if /i "%%~a" equ "RUNNING" set "DOCENTRICAX_AOSWEB_RUNNING=Yes"
	tasklist /fi "IMAGENAME eq batch.exe" | find /i "batch.exe">NUL
	if %ERRORLEVEL% equ 0 set "DOCENTRICAX_AOSBATCH_RUNNING=Yes"
	
:parse
    if "%~1"=="" goto :validate

    if /i "%~1"=="/?"         call :header & call :usage "%~2" & goto :end
    if /i "%~1"=="-?"         call :header & call :usage "%~2" & goto :end
    if /i "%~1"=="--help"     call :header & call :usage "%~2" & goto :end

    if /i "%~1"=="/v"         call :version      & goto :end
    if /i "%~1"=="-v"         call :version      & goto :end
    if /i "%~1"=="--version"  call :version full & goto :end

    shift
    goto :parse

:validate
	REM Check for mandatory Docentric AOS packages
	if not exist "%DOCENTRICAX_MODEL_MAIN_VERSION_DESCRIPTOR_FILE%" call :missing_model & goto :end
	if not exist "%DOCENTRICAX_MODEL_EXTENSION_VERSION_DESCRIPTOR_FILE%" call :missing_model & goto :end
	
	REM This model is optional - if not exist "%DOCENTRICAX_MODEL_EMAILS_VERSION_DESCRIPTOR_FILE%" call :missing_model & goto :end

	REM Check for elevated rights
	net.exe session 1>NUL 2>NUL || (call :elevated_rights & goto :end)

	goto main

:stop_aos_applications
	REM To recycle IIS web apps or IIS Express web apps rename the web.config to other name and then rename it back
	echo Stopping AOS applications	
	echo.
	if /i "%DOCENTRICAX_AOSWEB_RUNNING%" equ "Yes" (	
		echo   Stopping Internet services...
		iisreset /stop
		taskkill /IM w3wp.exe /F 1>NUL 2>NUL
		
		echo   Stopping AOS Web applications...
		move /Y "%DOCENTRICAX_AOSWebRootPath%\web.config" "%DOCENTRICAX_AOSWebRootPath%\___web.config">NUL
		move /Y "%DOCENTRICAX_AOSWebRootPath%\___web.config" "%DOCENTRICAX_AOSWebRootPath%\web.config">NUL
	) else echo   AOS Web not running
	
	if /i "%DOCENTRICAX_AOSBATCH_RUNNING%" equ "Yes" (
		echo   Stopping AOS Batch service...
		call net stop DynamicsAxBatch
		taskkill /IM batch.exe /F 1>NUL 2>NUL
	) else echo   AOS Batch service not running
	
	echo   Terminating Visual Studio related assembly locks
	taskkill /IM msvsmon.exe /F 1>NUL 2>NUL
	echo.
	TIMEOUT /T 2 /NOBREAK 1>NUL 2>NUL
	
	goto :eof

:start_aos_applications
	set "_aos_apps_start_needed="
	if /i "%DOCENTRICAX_AOSWEB_RUNNING%" equ "Yes" set "_aos_apps_start_needed=1"
	if /i "%DOCENTRICAX_AOSBATCH_RUNNING%" equ "Yes" set "_aos_apps_start_needed=1"
	if "%_aos_apps_start_needed%" neq "1" goto :eof
	
	echo.
	echo Starting AOS applications

	if /i "%DOCENTRICAX_AOSWEB_RUNNING%" equ "Yes" (
		REM JUST IN CASE SOMEONE TEMPERED WITH IIS WE FIRST STOP IT AND THEN WE RUN IT AGAIN
		iisreset /stop>NUL
		taskkill /IM w3wp.exe /F 1>NUL 2>NUL
		echo.		
		echo   Starting AOS Web applications and Internet services...
		iisreset /start
	)

	if /i "%DOCENTRICAX_AOSBATCH_RUNNING%" equ "Yes" (
		echo.
		echo   Starting AOS Batch service...
		call net start DynamicsAxBatch
	)
	
	goto :eof

:main
	call :header
	
	set "DOCENTRIC_AX_BUILD_MODELS=Yes"
	set /p "DOCENTRIC_AX_BUILD_MODELS=Enter NO to skip the model build process or press [ENTER] to continue [%DOCENTRIC_AX_BUILD_MODELS%] : "
	set "DOCENTRIC_AX_SYNC_DATABASE=Yes"
	set /p "DOCENTRIC_AX_SYNC_DATABASE=Enter NO to skip the database synchronization process or press [ENTER] to continue [%DOCENTRIC_AX_SYNC_DATABASE%] : "
	
	set /p "DOCENTRICAX_DB_HOSTNAME=Enter the D365FO database hostname or press [ENTER] for default [%DOCENTRICAX_DB_HOSTNAME%]: "
	
	set "DOCENTRIC_AX_BUILD_CONTINUE=No"
	set /p "DOCENTRIC_AX_BUILD_CONTINUE=Enter YES to start the build process or press [ENTER] to terminate [%DOCENTRIC_AX_BUILD_CONTINUE%] (NOTE: Build process might take long time to finish): "
	if /i "%DOCENTRIC_AX_BUILD_CONTINUE%" NEQ "YES" goto exit
	
	REM Stopping AOS applications
	call :stop_aos_applications
	if %ERRORLEVEL% neq 0 echo AOS applications can not be stopped. Terminating...& goto end
			
	if /i "%DOCENTRIC_AX_BUILD_MODELS%" NEQ "YES" goto main_db_sync
	REM Build labels
	echo Build Docentric AX labels started
	echo.
    call "%DOCENTRICAX_AOSTOOL_LABELC%" -metadata="%DOCENTRICAX_AOSPath%" -modelmodule="%DOCENTRICAX_MODEL_MAIN_MODULENAME%" -output="%DOCENTRICAX_MODEL_MAIN_PATH%\Resources" -outlog="%DOCENTRICAX_MODEL_MAIN_LOG_LABELC_OUTPUT_FILE%" -errlog="%DOCENTRICAX_MODEL_MAIN_LOG_LABELC_ERROUTPUT_FILE%"
	echo   Model: %DOCENTRICAX_MODEL_MAIN_MODULENAME% ^(for details see logs: %DOCENTRICAX_MODEL_MAIN_LOG_PATH%^)
	call "%DOCENTRICAX_AOSTOOL_LABELC%" -metadata="%DOCENTRICAX_AOSPath%" -modelmodule="%DOCENTRICAX_MODEL_EXTENSION_MODULENAME%" -output="%DOCENTRICAX_MODEL_EXTENSION_PATH%\Resources" -outlog="%DOCENTRICAX_MODEL_EXTENSION_LOG_LABELC_OUTPUT_FILE%" -errlog="%DOCENTRICAX_MODEL_EXTENSION_LOG_LABELC_ERROUTPUT_FILE%"
	echo   Model: %DOCENTRICAX_MODEL_EXTENSION_MODULENAME% ^(for details see logs: %DOCENTRICAX_MODEL_EXTENSION_LOG_PATH%^)
	call "%DOCENTRICAX_AOSTOOL_LABELC%" -metadata="%DOCENTRICAX_AOSPath%" -modelmodule="%DOCENTRICAX_MODEL_EMAILS_MODULENAME%" -output="%DOCENTRICAX_MODEL_EMAILS_PATH%\Resources" -outlog="%DOCENTRICAX_MODEL_EMAILS_LOG_LABELC_OUTPUT_FILE%" -errlog="%DOCENTRICAX_MODEL_EMAILS_LOG_LABELC_ERROUTPUT_FILE%"
	echo   Model: %DOCENTRICAX_MODEL_EMAILS_MODULENAME% ^(for details see logs: %DOCENTRICAX_MODEL_EMAILS_LOG_PATH%^)
	if exist "%DOCENTRICAX_MODEL_SSRSREPLICAS_VERSION_DESCRIPTOR_FILE%" (
		call %DOCENTRICAX_AOSTOOL_LABELC% -metadata="%DOCENTRICAX_AOSPath%" -modelmodule="%DOCENTRICAX_MODEL_SSRSREPLICAS_MODULENAME%" -output="%DOCENTRICAX_MODEL_SSRSREPLICAS_PATH%\Resources" -outlog="%DOCENTRICAX_MODEL_SSRSREPLICAS_LOG_LABELC_OUTPUT_FILE%" -errlog="%DOCENTRICAX_MODEL_SSRSREPLICAS_LOG_LABELC_ERROUTPUT_FILE%"
		echo   Model: %DOCENTRICAX_MODEL_SSRSREPLICAS_MODULENAME% ^(for details see logs: %DOCENTRICAX_MODEL_SSRSREPLICAS_LOG_PATH%^)
	)
	if exist "%DOCENTRICAX_MODEL_CBDREPLICAS_VERSION_DESCRIPTOR_FILE%" (
		call %DOCENTRICAX_AOSTOOL_LABELC% -metadata="%DOCENTRICAX_AOSPath%" -modelmodule="%DOCENTRICAX_MODEL_CBDREPLICAS_MODULENAME%" -output="%DOCENTRICAX_MODEL_CBDREPLICAS_PATH%\Resources" -outlog="%DOCENTRICAX_MODEL_CBDREPLICAS_LOG_LABELC_OUTPUT_FILE%" -errlog="%DOCENTRICAX_MODEL_CBDREPLICAS_LOG_LABELC_ERROUTPUT_FILE%"
		echo   Model: %DOCENTRICAX_MODEL_CBDREPLICAS_MODULENAME% ^(for details see logs: %DOCENTRICAX_MODEL_CBDREPLICAS_LOG_PATH%^)
	)
	if exist "%DOCENTRICAX_MODEL_WAREHOUSELABELS_VERSION_DESCRIPTOR_FILE%" (
		call %DOCENTRICAX_AOSTOOL_LABELC% -metadata="%DOCENTRICAX_AOSPath%" -modelmodule="%DOCENTRICAX_MODEL_WAREHOUSELABELS_MODULENAME%" -output="%DOCENTRICAX_MODEL_WAREHOUSELABELS_PATH%\Resources" -outlog="%DOCENTRICAX_MODEL_WAREHOUSELABELS_LOG_LABELC_OUTPUT_FILE%" -errlog="%DOCENTRICAX_MODEL_WAREHOUSELABELS_LOG_LABELC_ERROUTPUT_FILE%"
		echo   Model: %DOCENTRICAX_MODEL_WAREHOUSELABELS_MODULENAME% ^(for details see logs: %DOCENTRICAX_MODEL_WAREHOUSELABELS_LOG_PATH%^)
	)
	echo.
	echo Build Docentric AX labels finished
	
	REM Build models
	echo.
	echo Build Docentric AX models started
	echo.
	echo   *** Starting with model %DOCENTRICAX_MODEL_MAIN_MODELNAME%
	call "%DOCENTRICAX_AOSTOOL_XPPC%" -metadata="%DOCENTRICAX_AOSPath%" -compilermetadata="%DOCENTRICAX_AOSPath%" -xref -xrefSqlServer="%DOCENTRICAX_DB_HOSTNAME%" -xrefDbName="DYNAMICSXREFDB" -appBase="%DOCENTRICAX_AOSPath_BINFOLDER%" -modelmodule="%DOCENTRICAX_MODEL_MAIN_MODULENAME%" -referenceFolder="%DOCENTRICAX_AOSPath%" -refPath="%DOCENTRICAX_MODEL_MAIN_PATH%\bin" -output="%DOCENTRICAX_MODEL_MAIN_PATH%\bin" -log="%DOCENTRICAX_MODEL_MAIN_LOG_XPPC_OUTPUT_FILE%" -xmllog="%DOCENTRICAX_MODEL_MAIN_LOG_XPPC_XMLOUTPUT_FILE%"
	if %ERRORLEVEL% NEQ 0 echo   *** Model %DOCENTRICAX_MODEL_MAIN_MODELNAME% has failed ^(for details see logs: %DOCENTRICAX_MODEL_MAIN_LOG_PATH%^)^! & call :start_aos_applications & goto end
	echo   *** Model %DOCENTRICAX_MODEL_MAIN_MODELNAME% has succeeded ^(for details see logs: %DOCENTRICAX_MODEL_MAIN_LOG_PATH%^)

	echo   *** Starting to copy model %DOCENTRICAX_MODEL_MAIN_MODELNAME% static resources, if needed
	set "DOCENTRICAX_MODEL_MAIN_RESOURCE_PATH=%DOCENTRICAX_MODEL_MAIN_PATH%\%DOCENTRICAX_MODEL_MAIN_MODELNAME%\AxResource\ResourceContent\Html"
	if exist "%DOCENTRICAX_MODEL_MAIN_RESOURCE_PATH%" (
		echo.
		echo     ==^> Copying HTML resources
		call xcopy "%DOCENTRICAX_MODEL_MAIN_RESOURCE_PATH%" "%DOCENTRICAX_AOSWebRootPath%\Resources\Html" /Y /R /H /I & set ERRORLEVEL=0
	)
	set "DOCENTRICAX_MODEL_MAIN_RESOURCE_PATH=%DOCENTRICAX_MODEL_MAIN_PATH%\%DOCENTRICAX_MODEL_MAIN_MODELNAME%\AxResource\ResourceContent\Images"
	if exist "%DOCENTRICAX_MODEL_MAIN_RESOURCE_PATH%" (
		echo.
		echo     ==^> Copying IMAGE resources
		call xcopy "%DOCENTRICAX_MODEL_MAIN_RESOURCE_PATH%" "%DOCENTRICAX_AOSWebRootPath%\Resources\Images" /Y /R /H /I & set ERRORLEVEL=0
	)
	set "DOCENTRICAX_MODEL_MAIN_RESOURCE_PATH=%DOCENTRICAX_MODEL_MAIN_PATH%\%DOCENTRICAX_MODEL_MAIN_MODELNAME%\AxResource\ResourceContent\Scripts"
	if exist "%DOCENTRICAX_MODEL_MAIN_RESOURCE_PATH%" (
		echo.
		echo     ==^> Copying SCRIPT resources
		call xcopy "%DOCENTRICAX_MODEL_MAIN_RESOURCE_PATH%" "%DOCENTRICAX_AOSWebRootPath%\Resources\Scripts" /Y /R /H /I & set ERRORLEVEL=0
	)
	set "DOCENTRICAX_MODEL_MAIN_RESOURCE_PATH=%DOCENTRICAX_MODEL_MAIN_PATH%\%DOCENTRICAX_MODEL_MAIN_MODELNAME%\AxResource\ResourceContent\Styles"
	if exist "%DOCENTRICAX_MODEL_MAIN_RESOURCE_PATH%" (
		echo.
		echo     ==^> Copying STYLE resources
		call xcopy "%DOCENTRICAX_MODEL_MAIN_RESOURCE_PATH%" "%DOCENTRICAX_AOSWebRootPath%\Resources\Styles" /Y /R /H /I & set ERRORLEVEL=0
	)
	echo   *** Resources copied successfully.
	echo.
	
	echo   *** Starting with model %DOCENTRICAX_MODEL_EXTENSION_MODELNAME%
	call "%DOCENTRICAX_AOSTOOL_XPPC%" -metadata="%DOCENTRICAX_AOSPath%" -compilermetadata="%DOCENTRICAX_AOSPath%" -xref -xrefSqlServer="%DOCENTRICAX_DB_HOSTNAME%" -xrefDbName="DYNAMICSXREFDB" -appBase="%DOCENTRICAX_AOSPath_BINFOLDER%" -modelmodule="%DOCENTRICAX_MODEL_EXTENSION_MODULENAME%" -referenceFolder="%DOCENTRICAX_AOSPath%" -refPath="%DOCENTRICAX_MODEL_MAIN_PATH%\bin" -refPath="%DOCENTRICAX_MODEL_EXTENSION_PATH%\bin" -output="%DOCENTRICAX_MODEL_EXTENSION_PATH%\bin" -log="%DOCENTRICAX_MODEL_EXTENSION_LOG_XPPC_OUTPUT_FILE%" -xmllog="%DOCENTRICAX_MODEL_EXTENSION_LOG_XPPC_XMLOUTPUT_FILE%"
	if %ERRORLEVEL% NEQ 0 echo   *** Model %DOCENTRICAX_MODEL_EXTENSION_MODELNAME% has failed ^(for details see logs: %DOCENTRICAX_MODEL_EXTENSION_LOG_PATH%^)^! & call :start_aos_applications & goto end
	echo   *** Model %DOCENTRICAX_MODEL_EXTENSION_MODELNAME% has succeeded ^(for details see logs: %DOCENTRICAX_MODEL_EXTENSION_LOG_PATH%^)
	
	echo   *** Starting to copy model %DOCENTRICAX_MODEL_EXTENSION_MODELNAME% static resources, if needed
	set "DOCENTRICAX_MODEL_EXTENSION_RESOURCE_PATH=%DOCENTRICAX_MODEL_EXTENSION_PATH%\%DOCENTRICAX_MODEL_EXTENSION_MODELNAME%\AxResource\ResourceContent\Html"
	if exist "%DOCENTRICAX_MODEL_EXTENSION_RESOURCE_PATH%" (
		echo.
		echo     ==^> Copying HTML resources
		call xcopy "%DOCENTRICAX_MODEL_EXTENSION_RESOURCE_PATH%" "%DOCENTRICAX_AOSWebRootPath%\Resources\Html" /Y /R /H /I & set ERRORLEVEL=0
	)
	set "DOCENTRICAX_MODEL_EXTENSION_RESOURCE_PATH=%DOCENTRICAX_MODEL_EXTENSION_PATH%\%DOCENTRICAX_MODEL_EXTENSION_MODELNAME%\AxResource\ResourceContent\Images"
	if exist "%DOCENTRICAX_MODEL_EXTENSION_RESOURCE_PATH%" (
		echo.
		echo     ==^> Copying IMAGE resources
		call xcopy "%DOCENTRICAX_MODEL_EXTENSION_RESOURCE_PATH%" "%DOCENTRICAX_AOSWebRootPath%\Resources\Images" /Y /R /H /I & set ERRORLEVEL=0
	)
	set "DOCENTRICAX_MODEL_EXTENSION_RESOURCE_PATH=%DOCENTRICAX_MODEL_EXTENSION_PATH%\%DOCENTRICAX_MODEL_EXTENSION_MODELNAME%\AxResource\ResourceContent\Scripts"
	if exist "%DOCENTRICAX_MODEL_EXTENSION_RESOURCE_PATH%" (
		echo.
		echo     ==^> Copying SCRIPT resources
		call xcopy "%DOCENTRICAX_MODEL_EXTENSION_RESOURCE_PATH%" "%DOCENTRICAX_AOSWebRootPath%\Resources\Scripts" /Y /R /H /I & set ERRORLEVEL=0
	)
	set "DOCENTRICAX_MODEL_EXTENSION_RESOURCE_PATH=%DOCENTRICAX_MODEL_EXTENSION_PATH%\%DOCENTRICAX_MODEL_EXTENSION_MODELNAME%\AxResource\ResourceContent\Styles"
	if exist "%DOCENTRICAX_MODEL_EXTENSION_RESOURCE_PATH%" (
		echo.
		echo     ==^> Copying STYLE resources
		call xcopy "%DOCENTRICAX_MODEL_EXTENSION_RESOURCE_PATH%" "%DOCENTRICAX_AOSWebRootPath%\Resources\Styles" /Y /R /H /I & set ERRORLEVEL=0
	)
	echo   *** Resources copied successfully.
	
	echo.
	
	if exist "%DOCENTRICAX_MODEL_EMAILS_VERSION_DESCRIPTOR_FILE%" (
		echo   *** Starting with model %DOCENTRICAX_MODEL_EMAILS_MODELNAME%
		call "%DOCENTRICAX_AOSTOOL_XPPC%" -metadata="%DOCENTRICAX_AOSPath%" -compilermetadata="%DOCENTRICAX_AOSPath%" -xref -xrefSqlServer="%DOCENTRICAX_DB_HOSTNAME%" -xrefDbName="DYNAMICSXREFDB" -appBase="%DOCENTRICAX_AOSPath_BINFOLDER%" -modelmodule="%DOCENTRICAX_MODEL_EMAILS_MODULENAME%" -referenceFolder="%DOCENTRICAX_AOSPath%" -refPath="%DOCENTRICAX_MODEL_MAIN_PATH%\bin" -refPath="%DOCENTRICAX_MODEL_EMAILS_PATH%\bin" -output="%DOCENTRICAX_MODEL_EMAILS_PATH%\bin" -log="%DOCENTRICAX_MODEL_EMAILS_LOG_XPPC_OUTPUT_FILE%" -xmllog="%DOCENTRICAX_MODEL_EMAILS_LOG_XPPC_XMLOUTPUT_FILE%"
		if %ERRORLEVEL% NEQ 0 echo   *** Model %DOCENTRICAX_MODEL_EMAILS_MODELNAME% has failed ^(for details see logs: %DOCENTRICAX_MODEL_EMAILS_LOG_PATH%^)^! & call :start_aos_applications & goto end
		echo   *** Model %DOCENTRICAX_MODEL_EMAILS_MODELNAME% has succeeded ^(for details see logs: %DOCENTRICAX_MODEL_EMAILS_LOG_PATH%^)
	
		echo   *** Starting to copy model %DOCENTRICAX_MODEL_EMAILS_MODELNAME% static resources, if needed
		set "DOCENTRICAX_MODEL_EMAILS_RESOURCE_PATH=%DOCENTRICAX_MODEL_EMAILS_PATH%\%DOCENTRICAX_MODEL_EMAILS_MODELNAME%\AxResource\ResourceContent\Html"
		if exist "%DOCENTRICAX_MODEL_EMAILS_RESOURCE_PATH%" (
			echo.
			echo     ==^> Copying HTML resources
			call xcopy "%DOCENTRICAX_MODEL_EMAILS_RESOURCE_PATH%" "%DOCENTRICAX_AOSWebRootPath%\Resources\Html" /Y /R /H /I & set ERRORLEVEL=0
		)
		set "DOCENTRICAX_MODEL_EMAILS_RESOURCE_PATH=%DOCENTRICAX_MODEL_EMAILS_PATH%\%DOCENTRICAX_MODEL_EMAILS_MODELNAME%\AxResource\ResourceContent\Images"
		if exist "%DOCENTRICAX_MODEL_EMAILS_RESOURCE_PATH%" (
			echo.
			echo     ==^> Copying IMAGE resources
			call xcopy "%DOCENTRICAX_MODEL_EMAILS_RESOURCE_PATH%" "%DOCENTRICAX_AOSWebRootPath%\Resources\Images" /Y /R /H /I & set ERRORLEVEL=0
		)
		set "DOCENTRICAX_MODEL_EMAILS_RESOURCE_PATH=%DOCENTRICAX_MODEL_EMAILS_PATH%\%DOCENTRICAX_MODEL_EMAILS_MODELNAME%\AxResource\ResourceContent\Scripts"
		if exist "%DOCENTRICAX_MODEL_EMAILS_RESOURCE_PATH%" (
			echo.
			echo     ==^> Copying SCRIPT resources
			call xcopy "%DOCENTRICAX_MODEL_EMAILS_RESOURCE_PATH%" "%DOCENTRICAX_AOSWebRootPath%\Resources\Scripts" /Y /R /H /I & set ERRORLEVEL=0
		)
		set "DOCENTRICAX_MODEL_EMAILS_RESOURCE_PATH=%DOCENTRICAX_MODEL_EMAILS_PATH%\%DOCENTRICAX_MODEL_EMAILS_MODELNAME%\AxResource\ResourceContent\Styles"
		if exist "%DOCENTRICAX_MODEL_EMAILS_RESOURCE_PATH%" (
			echo.
			echo     ==^> Copying STYLE resources
			call xcopy "%DOCENTRICAX_MODEL_EMAILS_RESOURCE_PATH%" "%DOCENTRICAX_AOSWebRootPath%\Resources\Styles" /Y /R /H /I & set ERRORLEVEL=0
		)
		echo   *** Resources copied successfully.
			
		echo.
	)
	
	if exist "%DOCENTRICAX_MODEL_SSRSREPLICAS_VERSION_DESCRIPTOR_FILE%" (
		echo   *** Starting with model %DOCENTRICAX_MODEL_SSRSREPLICAS_MODELNAME%
		call "%DOCENTRICAX_AOSTOOL_XPPC%" -metadata="%DOCENTRICAX_AOSPath%" -compilermetadata="%DOCENTRICAX_AOSPath%" -xref -xrefSqlServer="%DOCENTRICAX_DB_HOSTNAME%" -xrefDbName="DYNAMICSXREFDB" -appBase="%DOCENTRICAX_AOSPath_BINFOLDER%" -modelmodule="%DOCENTRICAX_MODEL_SSRSREPLICAS_MODULENAME%" -referenceFolder="%DOCENTRICAX_AOSPath%" -refPath="%DOCENTRICAX_MODEL_SSRSREPLICAS_PATH%\bin" -refPath="%DOCENTRICAX_MODEL_MAIN_PATH%\bin" -output="%DOCENTRICAX_MODEL_SSRSREPLICAS_PATH%\bin" -log="%DOCENTRICAX_MODEL_SSRSREPLICAS_LOG_XPPC_OUTPUT_FILE%" -xmllog="%DOCENTRICAX_MODEL_SSRSREPLICAS_LOG_XPPC_XMLOUTPUT_FILE%"
		if %ERRORLEVEL% NEQ 0 echo   *** Model %DOCENTRICAX_MODEL_SSRSREPLICAS_MODELNAME% has failed ^(for details see logs: %DOCENTRICAX_MODEL_SSRSREPLICAS_LOG_PATH%^)^! & call :start_aos_applications & goto end
		echo   *** Model %DOCENTRICAX_MODEL_SSRSREPLICAS_MODELNAME% has succeeded ^(for details see logs: %DOCENTRICAX_MODEL_SSRSREPLICAS_LOG_PATH%^)
	
		echo   *** Starting to copy model %DOCENTRICAX_MODEL_SSRSREPLICAS_MODELNAME% static resources, if needed
		set "DOCENTRICAX_MODEL_SSRSREPLICAS_RESOURCE_PATH=%DOCENTRICAX_MODEL_SSRSREPLICAS_PATH%\%DOCENTRICAX_MODEL_SSRSREPLICAS_MODELNAME%\AxResource\ResourceContent\Html"
		if exist "%DOCENTRICAX_MODEL_SSRSREPLICAS_RESOURCE_PATH%" (
			echo.
			echo     ==^> Copying HTML resources
			call xcopy "%DOCENTRICAX_MODEL_SSRSREPLICAS_RESOURCE_PATH%" "%DOCENTRICAX_AOSWebRootPath%\Resources\Html" /Y /R /H /I & set ERRORLEVEL=0
		)
		set "DOCENTRICAX_MODEL_SSRSREPLICAS_RESOURCE_PATH=%DOCENTRICAX_MODEL_SSRSREPLICAS_PATH%\%DOCENTRICAX_MODEL_SSRSREPLICAS_MODELNAME%\AxResource\ResourceContent\Images"
		if exist "%DOCENTRICAX_MODEL_SSRSREPLICAS_RESOURCE_PATH%" (
			echo.
			echo     ==^> Copying IMAGE resources
			call xcopy "%DOCENTRICAX_MODEL_SSRSREPLICAS_RESOURCE_PATH%" "%DOCENTRICAX_AOSWebRootPath%\Resources\Images" /Y /R /H /I & set ERRORLEVEL=0
		)
		set "DOCENTRICAX_MODEL_SSRSREPLICAS_RESOURCE_PATH=%DOCENTRICAX_MODEL_SSRSREPLICAS_PATH%\%DOCENTRICAX_MODEL_SSRSREPLICAS_MODELNAME%\AxResource\ResourceContent\Scripts"
		if exist "%DOCENTRICAX_MODEL_SSRSREPLICAS_RESOURCE_PATH%" (
			echo.
			echo     ==^> Copying SCRIPT resources
			call xcopy "%DOCENTRICAX_MODEL_SSRSREPLICAS_RESOURCE_PATH%" "%DOCENTRICAX_AOSWebRootPath%\Resources\Scripts" /Y /R /H /I & set ERRORLEVEL=0
		)
		set "DOCENTRICAX_MODEL_SSRSREPLICAS_RESOURCE_PATH=%DOCENTRICAX_MODEL_SSRSREPLICAS_PATH%\%DOCENTRICAX_MODEL_SSRSREPLICAS_MODELNAME%\AxResource\ResourceContent\Styles"
		if exist "%DOCENTRICAX_MODEL_SSRSREPLICAS_RESOURCE_PATH%" (
			echo.
			echo     ==^> Copying STYLE resources
			call xcopy "%DOCENTRICAX_MODEL_SSRSREPLICAS_RESOURCE_PATH%" "%DOCENTRICAX_AOSWebRootPath%\Resources\Styles" /Y /R /H /I & set ERRORLEVEL=0
		)
		echo   *** Resources copied successfully.
	
		echo.
	)
	
	if exist "%DOCENTRICAX_MODEL_CBDREPLICAS_VERSION_DESCRIPTOR_FILE%" (
		echo   *** Starting with model %DOCENTRICAX_MODEL_CBDREPLICAS_MODELNAME%
		call "%DOCENTRICAX_AOSTOOL_XPPC%" -metadata="%DOCENTRICAX_AOSPath%" -compilermetadata="%DOCENTRICAX_AOSPath%" -xref -xrefSqlServer="%DOCENTRICAX_DB_HOSTNAME%" -xrefDbName="DYNAMICSXREFDB" -appBase="%DOCENTRICAX_AOSPath_BINFOLDER%" -modelmodule="%DOCENTRICAX_MODEL_CBDREPLICAS_MODULENAME%" -referenceFolder="%DOCENTRICAX_AOSPath%" -refPath="%DOCENTRICAX_MODEL_CBDREPLICAS_PATH%\bin" -refPath="%DOCENTRICAX_MODEL_MAIN_PATH%\bin" -output="%DOCENTRICAX_MODEL_CBDREPLICAS_PATH%\bin" -log="%DOCENTRICAX_MODEL_CBDREPLICAS_LOG_XPPC_OUTPUT_FILE%" -xmllog="%DOCENTRICAX_MODEL_CBDREPLICAS_LOG_XPPC_XMLOUTPUT_FILE%"
		if %ERRORLEVEL% NEQ 0 echo   *** Model %DOCENTRICAX_MODEL_CBDREPLICAS_MODELNAME% has failed ^(for details see logs: %DOCENTRICAX_MODEL_CBDREPLICAS_LOG_PATH%^)^! & call :start_aos_applications & goto end
		echo   *** Model %DOCENTRICAX_MODEL_CBDREPLICAS_MODELNAME% has succeeded ^(for details see logs: %DOCENTRICAX_MODEL_CBDREPLICAS_LOG_PATH%^)
		
		echo   *** Starting to copy model %DOCENTRICAX_MODEL_CBDREPLICAS_MODELNAME% static resources, if needed
		set "DOCENTRICAX_MODEL_CBDREPLICAS_RESOURCE_PATH=%DOCENTRICAX_MODEL_CBDREPLICAS_PATH%\%DOCENTRICAX_MODEL_CBDREPLICAS_MODELNAME%\AxResource\ResourceContent\Html"
		if exist "%DOCENTRICAX_MODEL_CBDREPLICAS_RESOURCE_PATH%" (
			echo.
			echo     ==^> Copying HTML resources
			call xcopy "%DOCENTRICAX_MODEL_CBDREPLICAS_RESOURCE_PATH%" "%DOCENTRICAX_AOSWebRootPath%\Resources\Html" /Y /R /H /I & set ERRORLEVEL=0
		)
		set "DOCENTRICAX_MODEL_CBDREPLICAS_RESOURCE_PATH=%DOCENTRICAX_MODEL_CBDREPLICAS_PATH%\%DOCENTRICAX_MODEL_CBDREPLICAS_MODELNAME%\AxResource\ResourceContent\Images"
		if exist "%DOCENTRICAX_MODEL_CBDREPLICAS_RESOURCE_PATH%" (
			echo.
			echo     ==^> Copying IMAGE resources
			call xcopy "%DOCENTRICAX_MODEL_CBDREPLICAS_RESOURCE_PATH%" "%DOCENTRICAX_AOSWebRootPath%\Resources\Images" /Y /R /H /I & set ERRORLEVEL=0
		)
		set "DOCENTRICAX_MODEL_CBDREPLICAS_RESOURCE_PATH=%DOCENTRICAX_MODEL_CBDREPLICAS_PATH%\%DOCENTRICAX_MODEL_CBDREPLICAS_MODELNAME%\AxResource\ResourceContent\Scripts"
		if exist "%DOCENTRICAX_MODEL_CBDREPLICAS_RESOURCE_PATH%" (
			echo.
			echo     ==^> Copying SCRIPT resources
			call xcopy "%DOCENTRICAX_MODEL_CBDREPLICAS_RESOURCE_PATH%" "%DOCENTRICAX_AOSWebRootPath%\Resources\Scripts" /Y /R /H /I & set ERRORLEVEL=0
		)
		set "DOCENTRICAX_MODEL_CBDREPLICAS_RESOURCE_PATH=%DOCENTRICAX_MODEL_CBDREPLICAS_PATH%\%DOCENTRICAX_MODEL_CBDREPLICAS_MODELNAME%\AxResource\ResourceContent\Styles"
		if exist "%DOCENTRICAX_MODEL_CBDREPLICAS_RESOURCE_PATH%" (
			echo.
			echo     ==^> Copying STYLE resources
			call xcopy "%DOCENTRICAX_MODEL_CBDREPLICAS_RESOURCE_PATH%" "%DOCENTRICAX_AOSWebRootPath%\Resources\Styles" /Y /R /H /I & set ERRORLEVEL=0
		)
		echo   *** Resources copied successfully.		
		
		echo.
	)
	
	if exist "%DOCENTRICAX_MODEL_WAREHOUSELABELS_VERSION_DESCRIPTOR_FILE%" (
		echo   *** Starting with model %DOCENTRICAX_MODEL_WAREHOUSELABELS_MODELNAME%
		call "%DOCENTRICAX_AOSTOOL_XPPC%" -metadata="%DOCENTRICAX_AOSPath%" -compilermetadata="%DOCENTRICAX_AOSPath%" -xref -xrefSqlServer="%DOCENTRICAX_DB_HOSTNAME%" -xrefDbName="DYNAMICSXREFDB" -appBase="%DOCENTRICAX_AOSPath_BINFOLDER%" -modelmodule="%DOCENTRICAX_MODEL_WAREHOUSELABELS_MODULENAME%" -referenceFolder="%DOCENTRICAX_AOSPath%" -refPath="%DOCENTRICAX_MODEL_WAREHOUSELABELS_PATH%\bin" -refPath="%DOCENTRICAX_MODEL_MAIN_PATH%\bin" -output="%DOCENTRICAX_MODEL_WAREHOUSELABELS_PATH%\bin" -log="%DOCENTRICAX_MODEL_WAREHOUSELABELS_LOG_XPPC_OUTPUT_FILE%" -xmllog="%DOCENTRICAX_MODEL_WAREHOUSELABELS_LOG_XPPC_XMLOUTPUT_FILE%"
		if %ERRORLEVEL% NEQ 0 echo   *** Model %DOCENTRICAX_MODEL_WAREHOUSELABELS_MODELNAME% has failed ^(for details see logs: %DOCENTRICAX_MODEL_WAREHOUSELABELS_LOG_PATH%^)^! & call :start_aos_applications & goto end
		echo   *** Model %DOCENTRICAX_MODEL_WAREHOUSELABELS_MODELNAME% has succeeded ^(for details see logs: %DOCENTRICAX_MODEL_WAREHOUSELABELS_LOG_PATH%^)
		
		echo   *** Starting to copy model %DOCENTRICAX_MODEL_WAREHOUSELABELS_MODELNAME% static resources, if needed
		set "DOCENTRICAX_MODEL_WAREHOUSELABELS_RESOURCE_PATH=%DOCENTRICAX_MODEL_WAREHOUSELABELS_PATH%\%DOCENTRICAX_MODEL_WAREHOUSELABELS_MODELNAME%\AxResource\ResourceContent\Html"
		if exist "%DOCENTRICAX_MODEL_WAREHOUSELABELS_RESOURCE_PATH%" (
			echo.
			echo     ==^> Copying HTML resources
			call xcopy "%DOCENTRICAX_MODEL_WAREHOUSELABELS_RESOURCE_PATH%" "%DOCENTRICAX_AOSWebRootPath%\Resources\Html" /Y /R /H /I & set ERRORLEVEL=0
		)
		set "DOCENTRICAX_MODEL_WAREHOUSELABELS_RESOURCE_PATH=%DOCENTRICAX_MODEL_WAREHOUSELABELS_PATH%\%DOCENTRICAX_MODEL_WAREHOUSELABELS_MODELNAME%\AxResource\ResourceContent\Images"
		if exist "%DOCENTRICAX_MODEL_WAREHOUSELABELS_RESOURCE_PATH%" (
			echo.
			echo     ==^> Copying IMAGE resources
			call xcopy "%DOCENTRICAX_MODEL_WAREHOUSELABELS_RESOURCE_PATH%" "%DOCENTRICAX_AOSWebRootPath%\Resources\Images" /Y /R /H /I & set ERRORLEVEL=0
		)
		set "DOCENTRICAX_MODEL_WAREHOUSELABELS_RESOURCE_PATH=%DOCENTRICAX_MODEL_WAREHOUSELABELS_PATH%\%DOCENTRICAX_MODEL_WAREHOUSELABELS_MODELNAME%\AxResource\ResourceContent\Scripts"
		if exist "%DOCENTRICAX_MODEL_WAREHOUSELABELS_RESOURCE_PATH%" (
			echo.
			echo     ==^> Copying SCRIPT resources
			call xcopy "%DOCENTRICAX_MODEL_WAREHOUSELABELS_RESOURCE_PATH%" "%DOCENTRICAX_AOSWebRootPath%\Resources\Scripts" /Y /R /H /I & set ERRORLEVEL=0
		)
		set "DOCENTRICAX_MODEL_WAREHOUSELABELS_RESOURCE_PATH=%DOCENTRICAX_MODEL_WAREHOUSELABELS_PATH%\%DOCENTRICAX_MODEL_WAREHOUSELABELS_MODELNAME%\AxResource\ResourceContent\Styles"
		if exist "%DOCENTRICAX_MODEL_WAREHOUSELABELS_RESOURCE_PATH%" (
			echo.
			echo     ==^> Copying STYLE resources
			call xcopy "%DOCENTRICAX_MODEL_WAREHOUSELABELS_RESOURCE_PATH%" "%DOCENTRICAX_AOSWebRootPath%\Resources\Styles" /Y /R /H /I & set ERRORLEVEL=0
		)
		echo   *** Resources copied successfully.		
		
		echo   *** Compiling reports. 
		call "%DOCENTRICAX_AOSTOOL_ReportsC%" -m="%DOCENTRICAX_AOSPath%" -mod="%DOCENTRICAX_MODEL_WAREHOUSELABELS_MODULENAME%" -l="%DOCENTRICAX_MODEL_WAREHOUSELABELS_LOG_REPORTC_OUTPUT_FILE%" -x="%DOCENTRICAX_MODEL_WAREHOUSELABELS_LOG_REPORTC_XMLOUTPUT_FILE%"
		if %ERRORLEVEL% NEQ 0 echo Report compilation for model has failed ^(for details see logs: %DOCENTRICAX_MODEL_WAREHOUSELABELS_LOG_PATH%^)^! & call :start_aos_applications & goto end
		echo   *** Reports compiled succesfully.
		
		echo   *** Starting to deploy related reports.	
		call  Powershell.exe -File "%DOCENTRICAX_AOSTOOL_DEPLOYALLREPORTSTOSSRS%" -LogFilePath "%DOCENTRICAX_MODEL_WAREHOUSELABELS_LOG_DEPLOYALLREPORTSTOSSRS_OUTPUT_FILE%" -PackageInstallLocation "%DOCENTRICAX_AOSPath%" -Module "%DOCENTRICAX_MODEL_WAREHOUSELABELS_MODULENAME%"
		if %ERRORLEVEL% NEQ 0 echo Report deployment for model has failed ^(for details see logs: %DOCENTRICAX_MODEL_WAREHOUSELABELS_LOG_PATH%^)^! & call :start_aos_applications & goto end
		echo   *** Reports deployed successfully.
		
		echo.
	)
	
	echo Build Docentric AX models finished
	
	:main_db_sync
	if /i "%DOCENTRIC_AX_SYNC_DATABASE%" NEQ "YES" goto restart_aos
	REM Sync database
    echo.
	echo Sync D365FO database started
	echo.
	call "%DOCENTRICAX_AOSTOOL_DBSyncEngine%" -syncmode=fullall -metadatabinaries="%DOCENTRICAX_AOSPath%" -connect="Data Source=%DOCENTRICAX_DB_HOSTNAME%;Initial Catalog=AxDB;Integrated Security=True;Enlist=True;Application Name=SyncEngine" > "%DOCENTRIC_AX_DBSYNC_LOGFILE%"
	if %ERRORLEVEL% NEQ 0 echo   *** Synchronize database has failed ^(for details see log: %DOCENTRIC_AX_DBSYNC_LOGFILE%^)^! & call :start_aos_applications & goto end
	for /f "tokens=*" %%a in ('find /i "Sync finished and took" ^< "%DOCENTRIC_AX_DBSYNC_LOGFILE%"') do echo %%~a
	echo   *** Synchronize database has succeeded ^(for details see log: %DOCENTRIC_AX_DBSYNC_LOGFILE%^)
	echo.
    echo Sync D365FO database finished
	
	:restart_aos
	REM Firing back AOS applications
	call :start_aos_applications
	if %ERRORLEVEL% neq 0 echo AOS applications can not started.& goto end
	
	goto exit
	
goto exit

:end
:exit
	endlocal

	IF %ERRORLEVEL% NEQ 0 TIMEOUT 10
	
	@ECHO ON